package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import com.yxinsur.imc.business.domain.ImcSupplierPersistencyConfig;
import com.yxinsur.imc.business.enums.*;
import com.yxinsur.imc.business.form.ImcSuppierPersistencyForm;
import com.yxinsur.imc.business.mapper.ImcSupplierPersistencyConfigMapper;
import com.yxinsur.imc.business.service.IImcSupplierPersistencyConfigService;
import com.yxinsur.imc.business.vo.PersistencyConfigVo;
import com.yxinsur.imc.common.core.domain.entity.SysOrg;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import com.yxinsur.imc.common.utils.StringUtils;
import com.yxinsur.imc.system.mapper.SysOrgMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 保险公司继续率计算规则配置Service业务层处理
 *
 * @author imc
 * @date 2021-09-22
 */
@Service
public class ImcSupplierPersistencyConfigServiceImpl extends ServiceImpl<ImcSupplierPersistencyConfigMapper, ImcSupplierPersistencyConfig> implements IImcSupplierPersistencyConfigService {
    @Autowired
    private ImcSupplierPersistencyConfigMapper imcSupplierPersistencyConfigMapper;
    @Autowired
    private SysOrgMapper sysOrgMapper;

    /**
     * 查询保险公司继续率计算规则配置
     *
     * @param id 保险公司继续率计算规则配置ID
     * @return 保险公司继续率计算规则配置
     */
    @Override
    public PersistencyConfigVo selectImcSupplierPersistencyConfigById(Long id) {
        return imcSupplierPersistencyConfigMapper.selectImcSupplierPersistencyConfigById(id);
    }

    /**
     * 查询保险公司继续率计算规则配置列表
     *
     * @param imcSuppierPersistencyForm 保险公司继续率计算规则配置
     * @return 保险公司继续率计算规则配置
     */
    @Override
    public List<PersistencyConfigVo> selectImcSupplierPersistencyConfigList(ImcSuppierPersistencyForm imcSuppierPersistencyForm) {
        ImcSupplierPersistencyConfig imcSupplierPersistencyConfig = new ImcSupplierPersistencyConfig();
        BeanUtils.copyProperties(imcSuppierPersistencyForm,imcSupplierPersistencyConfig);
        imcSupplierPersistencyConfig.setTenantId(SecurityUtils.getTenantId());
        imcSupplierPersistencyConfig.setIsDeleted(IsDeletedEnum.N.getCode());
        List<PersistencyConfigVo> persistencyConfigVos = imcSupplierPersistencyConfigMapper.selectImcSupplierPersistencyConfigList(imcSupplierPersistencyConfig);
        for (PersistencyConfigVo persistencyConfigVo : persistencyConfigVos) {
            //获得枚举值
            getEnumCode(persistencyConfigVo);
        }
        return persistencyConfigVos;
    }

    private void getEnumCode(PersistencyConfigVo persistencyConfigVo) {
        persistencyConfigVo.setPersistencyMethod(PersistencyMethodEnum.getCodeByMethod(persistencyConfigVo.getPersistencyMethod()));
        persistencyConfigVo.setPersistencyDimension(PersistencyDimesionEnum.getCodeByDimesion(persistencyConfigVo.getPersistencyDimension()));
        persistencyConfigVo.setPersistencyPeriodType(PersistencyPeriodEnum.getCodeByPeriod(persistencyConfigVo.getPersistencyPeriodType()));
        //分母
        String denominator = persistencyConfigVo.getDenominator();
        String[] denominatorArr = denominator.split(",");
        String[] denoAfterArr = new String[denominatorArr.length];
        for (int i = 0; i < denominatorArr.length; i++) {
            denoAfterArr[i]= PersistencyRuleEnum.getValueByCode(denominatorArr[i]);
        }
        persistencyConfigVo.setDenominator(StringUtils.join(denoAfterArr,","));
        //分子
        String numerator = persistencyConfigVo.getNumerator();
        String[] numeratorArr = numerator.split(",");
        String[] numeAfterArr = new String[numeratorArr.length];
        for (int i = 0; i < numeratorArr.length; i++) {
            numeAfterArr[i]= PersistencyRuleEnum.getValueByCode(numeratorArr[i]);
        }
        persistencyConfigVo.setNumerator(StringUtils.join(numeAfterArr,","));
        //机构
        String orgIds = persistencyConfigVo.getOrgIds();
        String[] orgIdsArr = orgIds.split(",");
        String[] orgNamesArr = new String[orgIdsArr.length];
        for (int i = 0; i < orgIdsArr.length; i++) {
            orgNamesArr[i] = sysOrgMapper.selectOrgById(Long.valueOf(orgIdsArr[i])).getOrgName();
        }
        persistencyConfigVo.setOrgNames(StringUtils.join(orgNamesArr,","));
    }

    /**
     * 新增保险公司继续率计算规则配置
     *
     * @param imcSuppierPersistencyForm 保险公司继续率计算规则配置
     * @return 结果
     */
    @Override
    public int insertImcSupplierPersistencyConfig(ImcSuppierPersistencyForm imcSuppierPersistencyForm) {
        ImcSupplierPersistencyConfig config = new ImcSupplierPersistencyConfig();
        BeanUtils.copyProperties(imcSuppierPersistencyForm,config);
        config.setCreateId(SecurityUtils.getUsername());
        config.setCreateTime(DateUtils.getNowDate());
        config.setUpdateId(SecurityUtils.getUsername());
        config.setUpdateTime(DateUtils.getNowDate());
        config.setTenantId(SecurityUtils.getTenantId());
        return imcSupplierPersistencyConfigMapper.insertImcSupplierPersistencyConfig(config);
    }

    /**
     * 修改保险公司继续率计算规则配置
     *
     * @param imcSuppierPersistencyForm 保险公司继续率计算规则配置
     * @return 结果
     */
    @Override
    public int updateImcSupplierPersistencyConfig(ImcSuppierPersistencyForm imcSuppierPersistencyForm) {
        ImcSupplierPersistencyConfig config = new ImcSupplierPersistencyConfig();
        BeanUtils.copyProperties(imcSuppierPersistencyForm,config);
        config.setUpdateTime(DateUtils.getNowDate());
        config.setUpdateId(SecurityUtils.getUsername());
        return imcSupplierPersistencyConfigMapper.updateImcSupplierPersistencyConfig(config);
    }

    /**
     * 批量删除保险公司继续率计算规则配置
     *
     * @param ids 需要删除的保险公司继续率计算规则配置ID
     * @return 结果
     */
    @Override
    public int deleteImcSupplierPersistencyConfigByIds(Long[] ids) {
        int count=0;
        for (Long id : ids) {
            ImcSupplierPersistencyConfig config = new ImcSupplierPersistencyConfig();
            config.setId(id);
            config.setIsDeleted(String.valueOf(id));
            count+=imcSupplierPersistencyConfigMapper.updateImcSupplierPersistencyConfig(config);
        }
        return count;
    }

    /**
     * 删除保险公司继续率计算规则配置信息
     *
     * @param id 保险公司继续率计算规则配置ID
     * @return 结果
     */
    @Override
    public int deleteImcSupplierPersistencyConfigById(String id) {
        return imcSupplierPersistencyConfigMapper.deleteImcSupplierPersistencyConfigById(id);
    }
}
